import { Button, DatePicker, Input, NavBar } from 'antd-mobile'
import Icon from '@/component/Icon'
import './index.scss'
import classNames from 'classnames'
import { billListData } from '@/constant/index'
import { useNavigate } from 'react-router-dom'
import { useState } from 'react'
import { getAddBill } from '@/store/modules/billStore'
import { useDispatch } from 'react-redux'
import dayjs from 'dayjs'


const New = () => {
  const navigate = useNavigate()
  const [selectType,setSelectType] = useState('pay')
  const [money,setMoney] = useState(0)
  const [useFor,setUseFor] = useState('')
  const [visible, setVisible] = useState(false)
  const [data,setData] = useState(dayjs())
  const dispatch = useDispatch()
  const clickSelectType = (type) => {
    setSelectType(type)
  }
  const changeInput = (value) => {
    setMoney(value)
  }
  //点击保存
  const clickSave = () => {
         let model={
          date:data,
          type:selectType,
          money:selectType==='pay' ? -money : +money,
          useFor:useFor       
        }
        dispatch(getAddBill(model))
  }
 
  return (
    <div className="keepAccounts">
      <NavBar className="nav" onBack={() => navigate(-1)}>
        记一笔
      </NavBar>
      
      <div className="header">
        <div className="kaType">
          <Button
            shape="rounded"
            onClick={() => clickSelectType('pay')}
            className={classNames({selected: selectType === 'pay'})}
          >
            支出
          </Button>
          <Button
            onClick={() => clickSelectType('income')}
            className={classNames({selected: selectType === 'income'})}
            shape="rounded"
          >
            收入
          </Button>
        </div>

        <div className="kaFormWrapper">
          <div className="kaForm">
            <div className="date">
              <Icon type="calendar" className="icon" />
              <span className="text" onClick={()=>setVisible(true)}>{dayjs(data).format('YYYY-MM-DD')}</span>
              <DatePicker
                className="kaDate"
                title="记账日期"
                max={new Date()}
                visible={visible}
                onClose={() => {
                  setVisible(false)
                }}
                onConfirm={val => {
                  setData(val)
                  setVisible(false)
                }}
              />
            </div>
            <div className="kaInput">
              <Input
                className="input"
                placeholder="0.00"
                type="number"
                value={money}
                onChange={changeInput}
              />
              <span className="iconYuan">¥</span>
            </div>
          </div>
        </div>
      </div>

      <div className="kaTypeList">
        {billListData[selectType].map(item => {
          return (
            <div className="kaType" key={item.type}>
              <div className="title">{item.name}</div>
              <div className="list">
                {item.list.map(item => {
                  return (
                    <div 
                      className={classNames(
                        'item',
                        {selected: useFor === item.type}
                      )}
                      key={item.type}
                      onClick={() => {
                        setUseFor(item.type)
                      }}
                    >
                      <div className="icon">
                        <Icon type={item.type} />
                      </div>
                      <div className="text">{item.name}</div>
                    </div>
                  )
                })}
              </div>
            </div>
          )
        })}
      </div>

      <div className="btns">
        <Button className="btn save" onClick={clickSave}>
          保 存
        </Button>
      </div>
    </div>
  )
}

export default New